package com.banach.prema.executor;

import com.banach.prema.dto.query.SosPageQuery;
import com.banach.prema.dto.vo.SosVO;
import com.banach.prema.repository.SosRepository;
import com.ego.base.api.LocationRemoteService;
import com.ego.base.dto.vo.AddressVO;
import com.ego.framework.core.context.UserHolder;
import com.ego.framework.dto.Response;
import com.ego.framework.enums.UserType;
import com.ego.framework.model.CustomizePage;
import com.ego.framework.model.LoginUser;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

/**
 * @Author ajun
 * @Desc
 * @Date 2025/3/24 11:33
 */
@Component
public class SosQueryExe {

    @Resource
    private SosRepository sosRepository;
    @Resource
    private LocationRemoteService locationRemoteService;

    @Value("${baidu.openapi.ak}")
    private String ak;


    public ModelAndView getViewById(HttpServletRequest request,Long sosId) {
        SosVO sos = sosRepository.selectById(sosId);
        ModelMap model = new ModelMap();
        model.put("sos", sos);
        model.put("ak", ak);
        model.put("contextPath", request.getContextPath());
        return new ModelAndView("/www/sos",model);
    }


    public Response<AddressVO> getCurrentLocation(Long sosId) {
        SosVO sos = sosRepository.selectById(sosId);
        Response<AddressVO> response = locationRemoteService.getAddress(sos.getLng(), sos.getLat());
		return response;
    }


    public Response<CustomizePage<SosVO>> selectPage(SosPageQuery pageQuery) {
        return Response.of(sosRepository.selectPage(pageQuery));
    }

    public Response<CustomizePage<SosVO>> selectMyPage() {
        SosPageQuery pageQuery = new SosPageQuery();
        LoginUser loginUser = UserHolder.getLoginUser();
        pageQuery.setUserId(loginUser.getId());
        return Response.of(sosRepository.selectPage(pageQuery));
    }

}
